<?php

/**
 * commentaire actions.
 *
 * @package    explications_informatiques
 * @subpackage search
 * @author     Guimezanes Cyrille
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class crontaskActions extends sfActions
{
  public function executeIndexing(sfWebRequest $request)
  {
	  $q = Doctrine_Query::create()
	 	->from('revisions n')
		->where('n.date_creation > ?',date("Y-m-d h:i:s",strtotime($advert["date_fin"],time()) - 24 * 60 * 60) );
	
		$newrevisions = $q->execute();
	  
	  $collection = new Doctrine_Collection('correctionsortho');
	  foreach($newrevisions as $revision)
	  {
		  $words = explode(" ",strip_tags(file_get_contents("revisions/".$revision["id_asso_platform"]."_".$revision["idrevisions"].".txt")));
		  $q2 = Doctrine_Query::create()
			->from('correctionsortho n')
			->where('n.id_asso = ?',$revision["id_asso_platform"]);
		  $q2->delete();
		  foreach($words as $word)
		  {
			  $q3 = Doctrine_Query::create()
				->from('noressearch n')
				->where('n.search = ?',$word);
			  $q3->delete();
			  if(strlen($word) > 3)
			  {
				  $asciiSum = 0;
				  for($i = 0; $i < strlen($word); $i++)
							$asciiSum += ord($word[$i]);
							
				  $index = new Correctionsortho();
				  $index->setWord($word);
				  $index->setIdAsso($revision["id_asso_platform"]);
				  $index->setAsciisum($asciiSum);
				  $collection->add($index);
			  }
		  }
	  }
	 
	  $collection->save();
	  //log
		sfCoreAutoload::logElem("Indexation: ".$collection->count()." mots", "System", date("Y-m-d h:i:s"));
	  die();
  }
  public function executeBackups(sfWebRequest $request)
  { 
	//-- Backup of the database
    $host="mysql51-36.perso"; // database host
	$dbuser="explicatsbdd"; // database user name
	$dbpswd="oEFhqZpq"; // database password
	$mysqldb="explicatsbdd"; // name of database
	$path = "http://www.explications-informatiques.fr/backups/"; // full server path to the directory where you want the backup files (no trailing slash)
	// modify the above values to fit your environment
	$filename = $path . "sql_".date("d") . ".sql";
	if ( file_exists($filename) ) unlink($filename);
	system("mysqldump --user=$dbuser --password=$dbpswd --host=$host $mysqldb > $filename",$result);
	
	//log
	sfCoreAutoload::logElem("Dump BDD: ".$filename." ".filesize($filename)." mots", "System", date("Y-m-d h:i:s"));
		
	die();
  }
}